package com.performanceManagement.controller;


import com.alibaba.fastjson.JSON;
import com.performanceManagement.dao.PerformanceManagementDao;
import com.performanceManagement.pojo.PerformanceManagement;
import com.performanceManagement.service.PerformanceManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController   //传递josn数据
@CrossOrigin(origins = "*",maxAge = 3600)  //允许所有访问请求这个接口
public class PerformanceManagementController {

    @Autowired
    PerformanceManagementService performanceManagementService;
    @Autowired
    PerformanceManagementDao performanceManagementDao;

    /**
     * 所有的绩效表
     * @return
     */
    @GetMapping("/all")
    public List<PerformanceManagement> all(){
        return  performanceManagementDao.findAll();
    }

    /**
     * 保存
     * @param performanceManagement
     */
    @PostMapping("/save")
    public void save(@RequestBody PerformanceManagement performanceManagement){
        System.out.println(performanceManagement);
        performanceManagementDao.save(performanceManagement);
    }


    /**
     * 导出excel
     * @param data
     */
    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody Map<String,Object> data){
        //因为前端传递过来的为多元组的Map对象，因此需要对其进行解析。

        //首先解析容易得到的文件名称
        String fileName= (String) data.get("fileName");

        //之后解析员工绩效信息，但get方法得到的ArrayList集合，因格式问题仍不能作为PerformanceManagement对象使用。
        List<Object> objects = (List<Object>) data.get("performanceManagements");

        //因此对其转换为json格式的字符串，之后通过json解析得到可以使用的PerformanceManagement对象。
        String json = JSON.toJSONString(objects);
        //解析得到PerformanceManagement的List集合
        List<PerformanceManagement>performanceManagements = JSON.parseArray(json,PerformanceManagement.class);

        //将其进行导出到Excel
        try {
            performanceManagementService.excelExport(performanceManagements,fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    使用默认格式导出
//    @PostMapping("/exportExcel")
//    public void save(@RequestBody List<PerformanceManagement>performanceManagements){
//
//        try {
//               performanceManagementService.excelExport(performanceManagements);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 通过部门名称查询
     * @param eDepartment
     * @return
     */
    @GetMapping("/queryByEDepartment")
    public List<PerformanceManagement> queryByEDepartment(String eDepartment){
        return  performanceManagementService.queryByEDepartment(eDepartment);
    }

    /**
     * 通过姓名查询
     * @param eName
     * @return
     */
    @GetMapping("/queryByEName")
    public List<PerformanceManagement> queryByEName(String eName){
        return performanceManagementService.queryByEName(eName);
    }

}
